package com.szholly.plug.form.excel.utils;

import java.util.regex.Pattern;

import com.szholly.data.general.GlobalParameter;

/**
 * 字符串操作类
 */
public class StringUtil {

	/**
	 * 判断一个字符串是否为null 或""或"  "
	 */
	public static boolean IsNullOrSpace(String value) {
		if (value == null || "".equals(value)) {
			return true;
		}
		if (value.trim().length() == 0) {
			return true;
		} else {
			return false;
		}
	}

	public static boolean HasLength(String value) {
		if (null == value || "".equals(value.trim())) {
			return false;
		} else {
			return true;
		}
	}

	// 对象转换成int型
	public static int objectToInt(Object obj) {
		int result = 0;
		if (null == obj || "".equals(obj)) {
			return 0;
		}
		try {
			result = Integer.parseInt(obj.toString());
		} catch (Exception e) {

		}
		return result;
	}

	public static boolean getBool(String strBool) {
		if (StringUtil.IsNullOrSpace(strBool))
			return false;
		else {
			strBool = strBool.trim().toUpperCase();
			if (strBool.equals("1") || strBool.equals("TRUE") || strBool.equals("是") || strBool.equals("YES"))
				return true;
			else
				return false;
		}
	}

	/**
	 * 判断一个字符串是否为null 或""或"  "
	 */
	public static boolean IsNullOrEmpty(String value) {
		if (value == null || "".equals(value.trim())) {
			return true;
		} else {
			return false;
		}
	}

	/**
	 * 判断一个字符串是否为null 或""或"  "
	 */
	public static boolean IsNotNullOrEmpty(String value) {
		if (value != null && !"".equals(value.trim()) && !"null".equals(value.trim().toLowerCase())) {
			return true;
		} else {
			return false;
		}
	}

	/**
	 * 判断一个字符串是否为null 或""或"  "
	 */
	public static boolean IsNullOrEmpty(Object value) {
		if (value == null || "".equals(value.toString())) {
			return true;
		} else {
			return false;
		}
	}

	/**
	 * 判断一个字符串是否为数字
	 */
	public static boolean isInteger(String str) {
		Pattern pattern = Pattern.compile("^[-\\+]?[\\d]*$");
		return pattern.matcher(str).matches();
	}

	/**
	 * 判断一个字符串是否为数字
	 */
	public static boolean isNumber(String str) {
		return str.matches("^[-+]?(([0-9]+)([.]([0-9]+))?|([.]([0-9]+))?)$");
	}

	/**
	 * 向左边添加指定的字符
	 */
	public static String PadLeft(String str, int padLength, char padLeftStr) {
		if (str.length() < padLength) {
			int padLengthIng = padLength - str.length();
			for (int i = 0; i < padLengthIng; i++) {
				str = padLeftStr + str;
			}
		}
		return str;
	}

	/**
	 * 向左边添加指定的字符
	 */
	public static String PadRight(String str, int padLength, char padLeftStr) {
		if (str.length() < padLength) {
			int padLengthIng = padLength - str.length();
			for (int i = 0; i < padLengthIng; i++) {
				str = str + padLeftStr;
			}
		}
		return str;
	}

	public static GlobalParameter getPara(String name, String type) {
		GlobalParameter para = new GlobalParameter();
		para.setFieldName(name);
		para.setFieldType(type);
		para.setOutParameter(false);
		return para;
	}

	public static String joinForSQL(String[] arr, String split) {
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < arr.length; i++) {
			sb.append("'");
			sb.append(arr[i].trim());
			sb.append("'");
			sb.append(",");
		}
		String newStr = sb.toString();
		if(newStr.length()>0)
			return newStr.substring(0, newStr.length() - 1);
		return "";
	}
}